﻿
function CreateEmptyOption()
{
    return new Option("请选择", -1);
}

findProvinceByNumber=function(number)
{
    for (var provinceIndex in provinces)
    {
        if( provinces[provinceIndex].number==number)
            return  provinces[provinceIndex];
    }
}

findCityByNumber = function (provinceNumber, cityNumber) {
    var province = this.findProvinceByNumber(provinceNumber);
    if (province)
    {
        for (var cityIndex in province.citys) {
            if (province.citys[cityIndex].number == cityNumber)
                return province.citys[cityIndex];
        }
    }
}

function bindAddressOption(provinceSelectStr, citySelectStr, districtSelectStr)
{
    var citySelect = $('#' + citySelectStr);
    var cityValue = citySelect.attr('curValue');
    var provinceSelect = $('#' + provinceSelectStr);
    var provinceValue = provinceSelect.attr('curValue');
    var districtSelect = $('#' + districtSelectStr);
    var districtValue = districtSelect.attr('curValue');

    //provinceSelect.append(CreateEmptyOption());
    for (var provinceIndex in provinces) {
        provinceSelect.append(new Option(provinces[provinceIndex].name, provinces[provinceIndex].number));
    }



    provinceSelect.change(function () {

        citySelect.empty();
        citySelect.append(CreateEmptyOption());
        districtSelect.empty();
        districtSelect.append(CreateEmptyOption());
        var selectProvince = findProvinceByNumber(provinceSelect.val());
        if (selectProvince) {
            for (var cityIndex in selectProvince.citys) {
                var city = selectProvince.citys[cityIndex];
                citySelect.append(new Option(city.name, city.number));
            }
        }
    });

    citySelect.change(function () {

        districtSelect.empty();
        districtSelect.append(CreateEmptyOption());
        var selectCity = findCityByNumber(provinceSelect.val(), citySelect.val());
        if (selectCity) {
            for (var areaIndex in selectCity.areas) {
                var area = selectCity.areas[areaIndex];
                districtSelect.append(new Option(area.name, area.number, false, districtValue == area.number));
            }
        }

    });

    provinceSelect.val(provinceValue);
    provinceSelect.change();
    citySelect.val(cityValue);
    citySelect.change();
    districtSelect.val(districtValue);
}

$(document).ready(function () {

    var datepickerOptions =
    {
        dateFormat: "yy-mm-dd",
        dayNamesMin: ["周日", "周一", "周二", "周三", "周四", "周五", "周六"],
        monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
        minDate: new Date().setDate(new Date().getDate() + 1),
    }


    $('#WorkInfo_WorkDate').datepicker(datepickerOptions);

    bindAddressOption('WorkInfo_ProvincialID', 'WorkInfo_CountyID', 'WorkInfo_CityID');
    bindAddressOption('AccountInfo_ProvincialID', 'AccountInfo_CountyID', 'AccountInfo_CityID');
    bindAddressOption('homeProvince', 'homeCity', 'homeDistrict');

    $("#submitBtn").click(function () {
        $('form').submit();
    });

    $("#backUrl").click(function () {
        window.location = $('#urlReferrer').val();
    });
});